package com.cskaoyan.service;

import com.cskaoyan.bean.po.BaseParam;
import com.cskaoyan.bean.po.MarketKeyword;
import com.cskaoyan.bean.po.MarketKeywordExample;
import com.cskaoyan.mapper.MarketKeywordMapper;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @program: SSMItem
 * @description 该Service是处理关键词功能的数据
 * @author: YY
 * @create: 2022-10-12 01:45
 **/
@Service
public class AdminKeywordServiceImpl implements AdminKeywordService {
    @Autowired
    MarketKeywordMapper mapper;

    //关键词的查询
    @Override
    public List<MarketKeyword> selectKeyword(BaseParam baseParam, String keyword, String url) {

        MarketKeywordExample example = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = example.createCriteria();
        example.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        criteria.andDeletedEqualTo(false);

        //判断url、keyword是否为空
        //如果为空或者为空格都会查询全部数据
        if (StringUtil.isEmpty(keyword) && StringUtil.isEmpty(url)) {
            List<MarketKeyword> list = mapper.selectByExample(example);
            return list;
        }
        //说明是模糊查询

        if (!StringUtil.isEmpty(keyword) && keyword.trim().length() != 0) {
            criteria.andKeywordLike("%" + keyword.trim() + "%");
        } else if (!StringUtil.isEmpty(url) && url.trim().length() != 0) {
            criteria.andKeywordLike("%" + url.trim() + "%");
        }
        List<MarketKeyword> list = mapper.selectByExample(example);
        return list;


    }

    @Override
    public MarketKeyword createKeyword(MarketKeyword keyword) {


        if (StringUtils.isEmpty(keyword.getIsHot())) {
            keyword.setIsHot(false);
        }
        if (StringUtils.isEmpty(keyword.getIsDefault())) {
            keyword.setIsDefault(false);
        }
        //通过keyword查询deleted
        Integer id = null;
        Integer rows = null;
        Integer deleted = mapper.selectDeletedByKeyword(keyword.getKeyword());

        if (!StringUtils.isEmpty(deleted)) {

            id = mapper.selectIdByKeyword(keyword.getKeyword());

            keyword.setId(id);
            keyword.setSortOrder(100);
            keyword.setAddTime(new Date());
            keyword.setUpdateTime(new Date());
            rows = mapper.updateByPrimaryKey(keyword);
            //说明已经存在，但是逻辑删除了
            mapper.updateByKeyword(keyword.getKeyword());

        }
        //插入数据

        if (StringUtils.isEmpty(rows)) {
            //说明没有插入
            keyword.setUpdateTime(new Date());
            keyword.setAddTime(new Date());
            mapper.insertSelective(keyword);
        }


        MarketKeyword marketKeyword = mapper.selectByPrimaryKey(keyword.getId());

        return marketKeyword;

    }


    @Override
    public MarketKeyword updateKeyword(MarketKeyword keyword) {

        keyword.setUpdateTime(new Date());
        mapper.updateByPrimaryKey(keyword);

        MarketKeyword keyword1 = mapper.selectByPrimaryKey(keyword.getId());

        return keyword1;

    }

    @Override
    public int deleteKeyword(MarketKeyword keyword) {

        //根据id删除对应的关键词
        int rows = mapper.updateDeletedByPrimaryKey(keyword.getId());

        return rows;
    }

    @Override
    public String selectIssueByQuestion(String keyword) {
        String keyword1 = mapper.selectKeywordByKeyword(keyword);
        return keyword1;
    }
}
